package com.example.myrecyclerview;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myrecyclerview.bean.Book;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    public ArrayList<Book> mBookList;

    public ArrayList<Book> getBookList() {
        return mBookList;
    }

    private static RecyclerViewAdapter recyclerViewAdapter;

    public RecyclerViewAdapter(ArrayList<Book> mBookList) {

        this.mBookList = mBookList;
        recyclerViewAdapter = this;
    }
    public RecyclerViewAdapter() {}

    /**
     * 默认的静态工厂
     * @return
     */
    public static RecyclerViewAdapter getDefaultRecyclerViewAdapter(){
        if(recyclerViewAdapter==null){
            recyclerViewAdapter = new RecyclerViewAdapter();//如果没有书籍数据，调用的时候就会报错，但在当前情况下不会因为在MainActivity中已经将书籍信息初始化了
        }
        return recyclerViewAdapter;
    }

    public void updateData(ArrayList<Book> mBookList) {
        this.mBookList = mBookList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        // 绑定数据
        holder.mTvBookName.setText(mBookList.get(position).getName());
        holder.mIvBookCover.setImageResource(mBookList.get(position).getCoverId());
    }

    @Override
    public int getItemCount() {

        return mBookList == null ? 0 : mBookList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {

        ImageView mIvBookCover;
        TextView mTvBookName;

        public ViewHolder(View itemView) {
            super(itemView);
            mIvBookCover = itemView.findViewById(R.id.item_iv_bookCover);
            mTvBookName = itemView.findViewById(R.id.item_tv_bookName);
            itemView.setOnCreateContextMenuListener(this);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuItem add = menu.add(Menu.NONE, 1, 1, "增加");
            MenuItem delete = menu.add(Menu.NONE, 2, 2, "删除");
            MenuItem update = menu.add(Menu.NONE, 3, 3, "修改");
            add.setOnMenuItemClickListener(this);
            delete.setOnMenuItemClickListener(this);
            update.setOnMenuItemClickListener(this);
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int position = getAdapterPosition();
            Intent intent = new Intent(MainActivity.getContext(), EditBookActivity.class);
            intent.putExtra("bookName",RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().get(position).getName());
            intent.putExtra("itemPosition",position);
            switch (item.getItemId()) {
                case 1:
//                    startActivity(intent);
//                    AlertDialog alertDialogAdd = new AlertDialog.Builder(BookListMainActivity.getContext()).create();
//                    alertDialogAdd.setTitle("新增书籍：");
//                    LayoutInflater layoutInflater = LayoutInflater.from(BookListMainActivity.getContext());
//                    View v = layoutInflater.inflate(R.layout.layout_add,null);
//                    alertDialogAdd.setView(v);
//                    EditText mEtBookName = v.findViewById(R.id.et_book_name);
//                    alertDialogAdd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            Log.d("<Test>","点击位置："+new Integer(position).toString());
//                            String bookName = mEtBookName.getText().toString();
//                            RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().add(new Book(bookName,R.drawable.book_1));
//                            RecyclerViewAdapter.getDefaultRecyclerViewAdapter().notifyDataSetChanged();
//                        }
//                    });
//                    alertDialogAdd.show();
                    intent.putExtra("startType",MyConst.ADD_BOOK);
                    MainActivity.getContext().startActivity(intent);
                    break;
                case 2:
                    AlertDialog alertDialogDelete = new AlertDialog.Builder(MainActivity.getContext()).create();
                    alertDialogDelete.setTitle("提示：");
                    alertDialogDelete.setMessage("确定要删除条目吗？");
                    alertDialogDelete.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
//                            Log.d("<Test>","点击位置："+new Integer(position).toString());
                            RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().remove(position);
                            RecyclerViewAdapter.getDefaultRecyclerViewAdapter().notifyDataSetChanged();
                        }
                    });
                    alertDialogDelete.show();
                    break;
                case 3:
//                    AlertDialog alertDialogUpdate = new AlertDialog.Builder(BookListMainActivity.getContext()).create();
//                    alertDialogUpdate.setTitle("新增书籍：");
//                    LayoutInflater layoutInflater2 = LayoutInflater.from(BookListMainActivity.getContext());
//                    View v2 = layoutInflater2.inflate(R.layout.layout_update,null);
//                    alertDialogUpdate.setView(v2);
//                    EditText mEtBookName2 = v2.findViewById(R.id.et_book_name);
//                    mEtBookName2.setText(RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().get(position).getName());
//                    alertDialogUpdate.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            Log.d("<Test>","点击位置："+new Integer(position).toString());
//                            String bookName = mEtBookName2.getText().toString();
//                            RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().get(position).setName(bookName);
//                            RecyclerViewAdapter.getDefaultRecyclerViewAdapter().notifyDataSetChanged();
//                        }
//                    });
//                    alertDialogUpdate.show();
                    intent.putExtra("startType",MyConst.UPDATE_BOOK);
                    MainActivity.getContext().startActivity(intent);
                    break;
            }
            return false;
        }
    }
}
